package net.expectx.pay.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import net.expectx.common.single.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class WeixinUtil {
    public static JSONObject getJsCode(String appId,String jsCode) throws Exception{
        Map<String, String> query = new HashMap<>(2);
        query.put("appid",appId);
        query.put("secret",getSecret(appId));
        query.put("js_code",jsCode);
        query.put("grant_type","authorization_code");
        HttpResponse response = HttpUtils.doGet("https://api.weixin.qq.com/","sns/jscode2session","GET",new HashMap<String,String>(2),query);
        JSONObject resultJSON=JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
        return resultJSON;
    }
    private static String getSecret(String appId){
        Map<String,String> map= Maps.newHashMapWithExpectedSize(7);
        map.put("wxa095e06a92f8d952","b32c21c6996f72a07305e0382cac67f2");
        map.put("wx9dec518566e910bd","d5f9e1f1aee8c692656b3b8ebc109e71");

        return  map.get(appId);
    }
    public static void main(String [] args) throws Exception{
        System.out.println(getJsCode("wxa095e06a92f8d952","001zxzRD0zWyoj2cMQRD0vVERD0zxzRB"));
    }
}
